package com.juyu.miliao.api.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.juyu.miliao.api.dao.mysql.BillDetailDao;
import com.juyu.miliao.api.dao.mysql.RedPacketDao;
import com.juyu.miliao.api.dao.mysql.UserCountDao;
import com.juyu.miliao.api.dao.mysql.UserWalletDao;
import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.domain.RedPacket;
import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.api.exception.WrongNoDepositException;
import com.juyu.miliao.api.util.PageUtil;

@Service
public class RedPacketService {
	@Resource
	private RedPacketDao redPacketDao;
	@Resource
	private UserWalletDao userWalletDao;
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	@Resource
	private BillDetailDao billDetailDao;
	@Resource
	private UserCountDao userCountDao;
	@Resource
	private RedisTemplateService redisTemplateService;

	public List<RedPacket> getRedPacketById(Integer id, Integer page, Integer pageSize) {
		int startNum = PageUtil.getOffset(page, pageSize);
		return redPacketDao.getRedPacketById(id,startNum,pageSize);
	}

	@Transactional(rollbackFor = Exception.class)
	synchronized public RedPacket computeRedPacket(RedPacket entity) {
		// 获取帐号钱包
		UserWallet userWallet = userWalletDao.getUserWalletById(entity.getUserId());
		// 判断钱包金币是否够用
		int userPacket = entity.getInDeposit();
		if (userPacket > userWallet.getDeposit())
			throw new WrongNoDepositException("金币不足");
		// 业务逻辑
		// 获取奖池值
		String strPool = stringRedisTemplate.opsForValue().get("redPacketPool");
		int pool = 0;
		if(strPool != null && !strPool.equals("") && !strPool.equals("null"))
			pool = Integer.valueOf(strPool).intValue();
		// 1.获取随机数范围
		int min = (int) (userPacket * 0.2);
		int max = 0;
		if (userPacket * 1.4 > userPacket * 0.6 + pool)
			max = (int) (userPacket * 0.6 + pool);
		else
			max = (int) (userPacket * 1.4);
		// 2.获取随机值
		int randNum = getRandom(min, max);
		// 3.计算奖池数值
		pool = (int) (userPacket * 0.6 - randNum + pool);
		stringRedisTemplate.opsForValue().set("redPacketPool", pool + "");
		entity.setOutDeposit(randNum);
		entity.setDifference(randNum - entity.getInDeposit());
		entity.setCreateTime(LocalDateTime.now());
//		redPacketDao.insertRedPacket(entity);
		// 保存消费记录
		BillDetail billDetail = new BillDetail();
		billDetail.setRecordType(6);// 消费类型6 幸运红包
		billDetail.setInUserId(0);// 赚取钻石用户id
		billDetail.setOutUserId(entity.getUserId());// 消费金币用户id
		billDetail.setDiamond(randNum);// 返回金币数
		billDetail.setSpend(entity.getInDeposit());// 消费金币数
		billDetailDao.insertBillDetail(billDetail);
		// 获取用户要消费的金币
		userWallet.setDeposit(entity.getDifference());
		userWallet.setUserId(entity.getUserId());
		userWalletDao.reviseDeposit(userWallet);
		if(entity.getInDeposit()-randNum > 0){
			//用户统计v值(6.19-土豪值只从充值和获取金币处计算)
			userCountDao.addVGradeSum(billDetail.getOutUserId(), entity.getInDeposit()-randNum);
			//排行榜统计(6.19-土豪值只从充值和获取金币处计算)
			redisTemplateService.setVUserSort(billDetail.getOutUserId(), entity.getInDeposit()-randNum);
		}
		return entity;
	}

	public static int getRandom(int min, int max) {
		Random rand = new Random();
		int randNum = rand.nextInt(max - min + 1) + min;
		return randNum;
	}

}
